import path from 'path'
import fs from 'fs'
const BaseRest = require('../rest.js')
module.exports = class extends BaseRest {
	/**
	 * 保存商品规格
	 * @returns {Promise.<void>}
	 */
  async postAction () {
	  const pk = await this.modelInstance.pk;
	  const data = this.post();
	  delete data[pk];
	  if (think.isEmpty(data)) {
		  return this.fail('没有要保存的数据');
	  }
	  const insertId = await this.modelInstance.add(data);
	  const imageDir = path.resolve(process.cwd(), this.config('imageDir'))
    const original = path.resolve(imageDir, 'original')
    const pc = path.resolve(imageDir, 'pc')
    const mobile = path.resolve(imageDir, 'mobile')
    const thumbnail = path.resolve(imageDir, 'thumbnail')
    if (!fs.existsSync(imageDir)) {
	    this.mkdir(original)
	    this.mkdir(pc)
	    this.mkdir(mobile)
	    this.mkdir(thumbnail)
    }
    const tempDir = this.config('imageTempDir')
    const fileName = data.thumbnail.split('thumbnail/')[1]
	  fs.renameSync(path.resolve(tempDir, 'original', fileName), path.resolve(original, fileName))
	  fs.renameSync(path.resolve(tempDir, 'pc', fileName), path.resolve(pc, fileName))
	  fs.renameSync(path.resolve(tempDir, 'mobile', fileName), path.resolve(mobile, fileName))
	  fs.renameSync(path.resolve(tempDir, 'thumbnail', fileName), path.resolve(thumbnail, fileName))
	  data.id = insertId
	  data.original_img = data.original_img.replace('/temp/', '/image/')
	  data.pc_img = data.pc_img.replace('/temp/', '/image/')
	  data.m_img = data.m_img.replace('/temp/', '/image/')
	  data.thumbnail = data.thumbnail.replace('/temp/', '/image/')
	  await this.modelInstance.update(data)
	  return this.success({id: insertId})
  }

	/**
	 * 修改商品规格
	 * @returns {Promise.<void>}
	 */
  async putAction () {
    const data = this.post();
    if (think.isEmpty(data)) {
      return this.fail('没有要保存的数据');
    }
    if (data.thumbnail.indexOf('/temp/') >= 0) {
    	const imageDir = this.config('imageDir')
      const tempDir = this.config('imageTempDir')
      const fileName = data.thumbnail.split('thumbnail/')[1]
      fs.renameSync(path.resolve(tempDir, 'original', fileName), path.resolve(original, fileName))
      fs.renameSync(path.resolve(tempDir, 'pc', fileName), path.resolve(pc, fileName))
      fs.renameSync(path.resolve(tempDir, 'mobile', fileName), path.resolve(mobile, fileName))
      fs.renameSync(path.resolve(tempDir, 'thumbnail', fileName), path.resolve(thumbnail, fileName))
      data.original_img = data.original_img.replace('/temp/', '/image/')
      data.pc_img = data.pc_img.replace('/temp/', '/image/')
      data.m_img = data.m_img.replace('/temp/', '/image/')
      data.thumbnail = data.thumbnail.replace('/temp/', '/image/')
		}
		let affectRow = await this.modelInstance.update(data)
		this.success({affectRow})
	}

	/**
	 * 分页获取规格列表
	 * @returns {Promise.<void>}
	 */
  async listAction() {
    const {page, pageSize, field, value} = this.get()
    let where = {}
    if (value) {
      where = {[field]: ['LIKE', `%${value}%`]}
    }
    let listPage = await this.modelInstance.where(where).page(page, pageSize).countSelect()
    this.success(listPage)
  }

  async saleAction () {
		const {id, is_sale} = this.post()
	  let affectRow = await this.modelInstance.update({id, is_sale: is_sale ? 1 : 0})
	  if (affectRow === 0) {
		  this.fail('数据不存在')
		  return
	  }
	  this.success(affectRow)
  }
};
